/**
 * 
 */
package controller;

import java.util.ArrayList;
import java.util.List;

import adapter.OptionAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.MenuItem;
import com.example.remotecontrol.R;

import common.Converter;
import common.DimensionHelper;
import components.AlarmManager;
import components.FragmentMediator;
import components.SMSCommandManager;
import components.SMSLogManager;
import components.SMSSendingCommandFactory;

import data.RCDevice;
import data.SMSCommand;

/**
 * @author User
 * 
 */
public class AlarmOptionFragment extends SherlockFragment implements OnItemSelectedListener{

	private ListView mListView;
	private List<RCDevice> mListItem = new ArrayList<RCDevice>();
	private OptionAdapter adapter;
	/**
	 * 
	 */
	public AlarmOptionFragment() {
		// TODO Auto-generated constructor stub
		//mListItem.add(new String[] { "1", "1.1" });
//		Alarm a1 = new Alarm();
//		a1.setName("Device 1");
//		a1.setAlert(true);
//		mListItem.add(a1);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.option_fragment, container,
				false);
		mListView = (ListView) view.findViewById(R.id.list_view);
	
		mListItem = AlarmManager.sharedAlarmList(getActivity()).getDevices();
		adapter = new OptionAdapter(getActivity(), mListItem);

		mListView.setAdapter(adapter);
	
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
			
			}
		});
		
		setHasOptionsMenu(true);
		return view;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
		getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(true);
		getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(true);
		getView().findViewById(R.id.root_layout).setPadding(
				0, DimensionHelper.getPxFromDp(getActivity(), 50), 0, 0);
		
		
		mListItem = AlarmManager.sharedAlarmList(getActivity()).getDevices();
		adapter.notifyDataSetChanged();
		
		//AlarmManager.changeAlertState(getActivity(), 1, true);
		
	}
	
	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.alarm_manager, menu);
	}
	
	@Override
	public void onPrepareOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// TODO Auto-generated method stub
		super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_settings:
			//TODO: send turn off alert
			SMSCommand smsCommand = SMSSendingCommandFactory.createSMSCommandWithConfiguredPhone(
					getActivity(), SMSCommand.CMD_CODE_TURN_OFF_ALARM, "");
			SMSCommandManager.sendMessageWithDialog(getActivity(), smsCommand);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
